package com.fs.common.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.fs.common.baseMapper.IntegralMapper;
import com.fs.common.baseMapper.UserMapper;
import com.fs.common.pojo.Integral;
import com.fs.common.pojo.Order;
import com.fs.common.pojo.Product;
import com.fs.common.pojo.User;
import com.fs.common.service.UserService;
import com.fs.common.yzMapper.ProductMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author DeveloperKK
 * @desc
 * @creationTime 2019-08-15 09:44
 * @projectName yuezi-menu
 * @packageName com.fs.common.service.impl
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private IntegralMapper integralMapper;

    @Override
    public boolean rechargeCount(String userId, Order order) {
        User user = userMapper.selectById(userId);
        Product product = productMapper.selectById(order.getProductId());
        if(!product.getType().equals("basic")){
            String startTime = user.getStartTime();
            String endTime = user.getEndTime();
            if(StringUtils.isBlank(startTime)){
                user.setStartTime(DateUtil.today());
            }
            if(StringUtils.isBlank(user.getStartTimeRecord())){
                user.setStartTimeRecord(DateUtil.today());
            }
            String date = DateUtil.formatDate(new Date());
            if(StringUtils.isBlank(endTime)){
                endTime = date;
            }else{
                long betweenDay = DateUtil.between(DateUtil.parse(endTime), DateUtil.parse(date), DateUnit.DAY);
                if(betweenDay < 0){
                    endTime = date;
                }
            }
            DateTime dateTime = new DateTime(endTime, DatePattern.NORM_DATE_FORMAT);
            user.setEndTime((dateTime.year()+product.getYearCount())+"-"+dateTime.monthStartFromOne()+"-"+dateTime.dayOfMonth());
        }
        user.setCount(user.getCount() + order.getCheckNumber());
        user.setBuyCount(user.getBuyCount()+order.getCheckNumber());
        user.setProductId(product.getId());
        return userMapper.updateById(user) == 1;
    }

    @Override
    public boolean deductionCount(String userId) {
        User user = userMapper.selectById(userId);
        if (user.getCount() > 0) {
            user.setCount((user.getCount()-1));
        }
        return userMapper.updateById(user) == 1;
    }

    @Override
    public boolean rechargeIntegral(User user, Order order) {
        Integer integral = new Double((order.getTotalFee()/100)*0.12).intValue();
        if(integral <= 0){
            integral = 1;
        }
        user.setIntegral(user.getIntegral()+integral);
        Integral integrals = new Integral();
        integrals.setIntegral(50);
        integrals.setType(1);
        integrals.setUserId(user.getId());
        integrals.setRemark("购买："+order.getOrderName());
        integralMapper.insert(integrals);
        return userMapper.updateById(user) == 1;
    }
}
